/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package server;

import java.sql.SQLException;

public class Main
{
	//TODO: nick v clientu
	//TODO: vytvoreni hesla v daemonu
	private static Server server = null;
	private static Daemon daemon = null;

	public static void main(String[] args)
	{
		Sifrovani.generatePhrase(); // Vygenerovani sifrovaci fraze
		
		// Zapnuti hlavnich vlaken serveru a daemona a inicializace pripojeni do mysql
		try
		{
			Mysql.getInstance(); // Inicializace c3p0 modulu pro komunikaci s mysql
			server = new Server();
			server.start();
			daemon = new Daemon();
			daemon.start();
			System.out.println("Server started.");
		}
		catch (SQLException e)
		{
			e.printStackTrace();
			System.exit(-1);
		}
	}

	// Metoda pro restartovani/vypnuti serveru
	public static void restart(boolean restart)
	{
		if (restart)
		{
			System.out.print("Restarting server: ");
			if (server != null)
				server.shutdown();
			server = new Server();
			server.start();
			Sifrovani.generatePhrase();
			System.out.println("Done");
		}
		else
		{
			System.out.println("Shutting down server.");
			System.exit(0);
		}
	}
}
